#! /usr/bin/env python
# -*- coding:utf-8 -*-

from Models import PlayGameModelProxy
import notifications
from puremvc.interfaces import IMediator
from puremvc.patterns.mediator import Mediator
from utils import ChatLogger

__author__ = 'CJay'


class QMLMediator(Mediator, IMediator):
    NAME = "QMLMediator"
    def __init__(self, viewComponent):
        super(QMLMediator, self).__init__(QMLMediator.NAME, viewComponent)
        #: @type: PlayGameModelProxy
        self.gameproxy = None
        self.root = viewComponent.rootObject()
#        self.actions_model = model = MyModel(data=[])
#        self.viewComponent.rootContext().setContextProperty("actionsModel",model)


    def listNotificationInterests(self):
        return [notifications.STARTGAME,
                notifications.NEWTURN
        ]

    def handleNotification(self,notification):
        if not self.gameproxy: self.gameproxy = self.facade.retrieveProxy(PlayGameModelProxy.NAME)

        if notification.getName() == notifications.NEWTURN:
            pass
        if notification.getName() == notifications.STARTGAME:
            self.root.setPlayerName("CJay")
            self.root.setCPUName("Katya")

class BalancePanelMediator(Mediator, IMediator):
    NAME = "BalancePanelMediator"
    def __init__(self, viewComponent):
        super(BalancePanelMediator,self).__init__(BalancePanelMediator.NAME, viewComponent)
        self.root = viewComponent
        self._gameproxy = None

    def updateCards(self, p_target):
        if p_target == "player":
            player_cards = self.gameproxy().getPCards()
        if p_target == "table":
            player_cards = self.gameproxy().getCards()
        for i in player_cards:
            self.root.addCard(p_target,i[0],int(i[1:]))

    def gameproxy(self):
        if not self._gameproxy: self._gameproxy = self.facade.retrieveProxy(PlayGameModelProxy.NAME)
        return self._gameproxy

    def listNotificationInterests(self):
        return [
            notifications.ACTION,
            notifications.STARTGAME,
            notifications.CHANGECPUMONEY,
            notifications.ADDCARD,
            notifications.FINISH
        ]

    def handleNotification(self,notification):
        if notification.getName() == notifications.ADDCARD:
            self.updateCards(notification.getBody())
        if notification.getName() == notifications.CHANGECPUMONEY:
            c_money = self.gameproxy().getCMoney()
            self.root.setCPUMoney("%d $"%c_money)
        if notification.getName() == notifications.ACTION:
            p_money = self.gameproxy().getPMoney()
            self.root.setPlayerMoney("%d $"%p_money)
        if notification.getName() == notifications.STARTGAME:
            p_money = self.gameproxy().getPMoney()
            self.root.setPlayerMoney("%d $"%p_money)
            cpu_money = self.gameproxy().getCMoney()
            self.root.setCPUMoney("%d $"%cpu_money)
        if notification.getName() == notifications.FINISH:
            combination = self.gameproxy().getPlayerCombination()
            self.root.showWinLoose("Your combination is: %s"%str(combination))


class ActionPanelMediator(Mediator, IMediator):
    NAME = "ActionPanelMediator"
    def __init__(self, viewComponent):
        super(ActionPanelMediator,self).__init__(ActionPanelMediator.NAME, viewComponent)
        self.root = viewComponent
        self._gameproxy = None
        self.root.action.connect(self.move)

    def gameproxy(self):
        if not self._gameproxy: self._gameproxy = self.facade.retrieveProxy(PlayGameModelProxy.NAME)
        return self._gameproxy

    def move(self,name):
        self.sendNotification(notifications.ACTION,(name,str(self.gameproxy().getPutPMoney())))

    def listNotificationInterests(self):
        return [
            notifications.PLAYER_TURN,
            notifications.ACTION,
            notifications.FINISH
        ]

    def handleNotification(self,notification):
        if notification.getName() == notifications.PLAYER_TURN:
            actions = notification.getBody().possible_actions
            self.root.fill.emit(actions)
        if notification.getName() == notifications.FINISH:
            self.root.clear.emit()

class BankPanelMediator(Mediator, IMediator):
    NAME = "BankPanelMediator"
    def __init__(self, viewComponent):
        super(BankPanelMediator,self).__init__(BankPanelMediator.NAME, viewComponent)
        self.root = viewComponent
        self.root.changeMoney.connect(self.incMoney)
        self._gameproxy = None

    def gameproxy(self):
        if not self._gameproxy: self._gameproxy = self.facade.retrieveProxy(PlayGameModelProxy.NAME)
        return self._gameproxy

    def incMoney(self,p_value):
        self.gameproxy().incrasePutPMoney(p_value)


    def listNotificationInterests(self):
        return [
            notifications.CHANGEBANKMONEY,
            notifications.PLAYER_TURN,
            notifications.STARTGAME,
            notifications.ACTION,
        ]
    def handleNotification(self,notification):
        if notification.getName() == notifications.STARTGAME:
            self.root.setBankMoney(self.gameproxy().getBankMoney())
        if notification.getName() == notifications.CHANGEBANKMONEY:
            self.root.setLabel(notification.getBody())
        if notification.getName() == notifications.PLAYER_TURN:
            min = self.gameproxy().getMin()
            self.root.setLabel(min)
        if notification.getName() == notifications.ACTION:
            self.root.setBankMoney(self.gameproxy().getBankMoney())




class ChatPanelMediator(Mediator, IMediator):
    NAME = "ChatPanelMediator"
    def __init__(self, viewComponent):
        super(ChatPanelMediator,self).__init__(ChatPanelMediator.NAME, viewComponent)
        self.root = viewComponent
        chatLogger = ChatLogger.getInstance()
        chatLogger.setMediator(self)

    def log(self,p_text):
        self.root.append(p_text)


    def listNotificationInterests(self):
        return [
        ]
    def handleNotification(self,notification):
        if notification.getName() == notifications.STARTGAME:
            self.log(u'Игра началась\n\n')
        if notification.getName() == notifications.PLAYER_TURN:
            self.log(u"Ваш ход: ")
        if notification.getName() == notifications.ACTION:
            self.log(u"%s: %s\n"%(notification.getBody()))
        if notification.getName() == notifications.CPU_TURN:
            self.log(u"Ход Блудницы:"%notification.getBody())
        if notification.getName() == notifications.CPU_ACTION:
            self.log(u"%s: %s\n"%(notification.getBody()))
        if notification.getName() == notifications.ENDTURN:
            self.log(u"Окончился раунд\n")
        if notification.getName() == notifications.FINISH:
            self.log(u"Игра окончена\n")



class GameTableMediator(Mediator, IMediator):
    NAME = "GameTableMediator"
    def __init__(self, viewComponent):
        super(GameTableMediator, self).__init__(GameTableMediator.NAME, viewComponent)

    def listNotificationInterests(self):
        return [notifications.UNDRESS,
                notifications.DRINK,
                notifications.SAVEGAME,
                notifications.LOADGAME,
                notifications.STARTGAME,
                notifications.NEWTURN,
                notifications.ADDCARD,
                notifications.BET,
                notifications.CALL,
                notifications.RAISE,
                notifications.CHECK,
                notifications.FOLD,
                notifications.BLIND,
                notifications.DOUBLEBLIND,
                notifications.WIN,
                notifications.LOOSE,
                notifications.FINISH,
                notifications.ENDTURN]

    def handleNotification(self,notification):
        if notification.getName() == notifications.DRINK:
            raise NotImplementedError, "I chose not to implement this yeat..."
            pass




class VideoMediator(Mediator, IMediator):
    NAME = 'VideoMediator'

    def __init__(self, viewComponent):
        super(VideoMediator, self).__init__(VideoMediator.NAME, viewComponent)
#        self.viewComponent.Bind(wx.EVT_TEXT_ENTER, self.onSubmit, self.viewComponent.inputFieldTxt)

    def listNotificationInterests(self):
        return [notifications.FULLSCREEN, notifications.NORMALSCREEN, notifications.PLAYVIDEO]

    def handleNotification(self,notification):
        if notification.getName() == notifications.FULLSCREEN:
            print "go to full screen"
            data = notification.getBody()
            self.viewComponent.setFullScreen()
        if notification.getName() == notifications.PLAYVIDEO:
            data = notification.getBody()
            print "i play video ", data
            self.viewComponent.play()
        if notification.getName() == notifications.NORMALSCREEN:
            print "go to full screen"
            data = notification.getBody()
            self.viewComponent.setFullScreen()          #TODO: change this on "normal screen"